package cn.aihongbo.kmp.pages

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.Text
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import cn.aihongbo.kmp.data.repository.BicsRepository
import cn.aihongbo.kmp.data.vm.HomeViewModel
import cn.aihongbo.kmp.pages.home.HomeOperationGiftView
import cn.aihongbo.kmp.pages.home.HomeOrderEnterView
import cn.aihongbo.kmp.pages.home.HomeRedHotBanner
import cn.aihongbo.kmp.pages.home.HomeTopBannerView
import cn.aihongbo.kmp.pages.home.HomeTravelView
import cn.aihongbo.kmp.pages.order.ChooseCityView
import kotlinx.coroutines.launch
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
@Preview
fun HomePage() {

    val viewModel = viewModel { HomeViewModel(BicsRepository()) }
    val sheetState = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()

    Box {
        LazyColumn(Modifier.background(Color(0xFFF2F5F6))) {
            item {
                Box {
                    //顶部轮播图
                    HomeTopBannerView(viewModel)

                    HomeOrderEnterView(viewModel, scope, sheetState)
                }
            }
            item {
                Text("当季热门", modifier = Modifier.padding(16.dp))
                HomeRedHotBanner(viewModel)
            }

            item {
                HomeOperationGiftView(viewModel)
            }

            item {
                Text("经典玩法", modifier = Modifier.padding(16.dp))
                HomeTravelView(viewModel)
            }
        }
        ModalBottomSheetLayout(
            sheetShape = RoundedCornerShape(topStart = 12.dp, topEnd = 12.dp),
            sheetState = sheetState,
            sheetContent = {
                ChooseCityView {
                    scope.launch {
                        viewModel.selectedCity.value = it
                        sheetState.hide()
                    }
                }
            }) {}
    }


    LaunchedEffect(Unit) {
        viewModel.loadBanners()
        viewModel.loadRedHotBanner()
        viewModel.loadQueryTravelDict()
        viewModel.loadOperationGift()
    }
}